/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador;

/**
 *
 * @author Fabian
 */
import connection.Connection;
import dao.UsuarioJpaController;


import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import javax.persistence.EntityManagerFactory;
import modelo.Usuario;

/**
 *
 * @author Fabian
 */
@ManagedBean(name = "Controlador")
@SessionScoped
public class Controlador extends Connection {

    public EntityManagerFactory factory;
    public String login;
    public String Password;
    public Connection conexion;

    public String getLogin() {
        return login;
    }

    public void setLogin(String x) {
        this.login = x;
    }

    public Controlador() {
    }

    public void inicializar() {
//         conexion=Connection.getConnection();
        factory = Connection.getEmf();
    }

    public String validar() {
        
        UsuarioJpaController daoUsuarios = new UsuarioJpaController(this.factory);
        String valor = "error?faces-redirect=true";
        Usuario u = daoUsuarios.findUsuario(this.login);
        if (u != null) {
            if (this.login.equalsIgnoreCase(u.getLogin()) && this.Password.equalsIgnoreCase(u.getContrasena()) && u.getEstado().equalsIgnoreCase("activo")) {

                if (u.getTipousuario().equalsIgnoreCase("profesor")) {
                    valor = "Profesor?faces-redirect=true";
                    return valor;
                } else if (u.getTipousuario().equalsIgnoreCase("decano")) {
                    valor = "Decano?faces-redirect=true";
                    return valor;
                } else if (u.getTipousuario().equalsIgnoreCase("jefe")) {
                    valor = "Jefe?faces-redirect=true";
                    return valor;
                } else if (u.getTipousuario().equalsIgnoreCase("evaluador")) {
                    valor = "evaluador?faces-redirect=true";
                    return valor;
                } else if (u.getTipousuario().equalsIgnoreCase("root")) {
                    valor = "Administrador?faces-redirect=true";
                    return valor;
                }

            } else {
                return valor;
            }

        }
        return valor;

    }

    public String getPassword() {
        return Password;
    }

    public void setPassword(String Password) {
        this.Password = Password;
    }
}
